package cn.Liujialin.org.service.impl;

import cn.Liujialin.basic.service.impl.BeanServiceImpl;
import cn.Liujialin.cation.BusinessException;
import cn.Liujialin.org.domain.Employee;
import cn.Liujialin.org.domain.Shop;
import cn.Liujialin.org.mapper.EmployeeMapper;
import cn.Liujialin.org.mapper.ShopMapper;
import cn.Liujialin.org.service.IShopService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class ShopServiceImpl extends BeanServiceImpl<Shop> implements IShopService{
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Override
    public void settledIn(Shop shop) throws BusinessException {
       //能否注册需要两大步骤
        //一，校验用户名是否为空和是否已经注册过（这里先需要校验三个字段，比如用户名，邮箱，电话等）
        /*StringUtils 这里是springframework 不是jdbc  为空这返回true
        * 自定义业务异常 这是用户操作出现的异常 我们后端没有这个异常所以要抛出给用户*/
        if (StringUtils.isEmpty(shop.getName())){
            throw new BusinessException("已经入驻，请直接登陆！如果忘记了请找回密码！");

        }
        //需要校验数据
        Employee admin = shop.getAdmin();
        Employee employee = employeeMapper.findByUsername(admin.getUsername());
        if (employee != null){
            throw new  BusinessException("该店铺已经入住");
        }
        //二需要保存数据
        //employee里面保存店长数据  一定要返回自增id
        employeeMapper.save(admin);
        //Shop里面保存商店数据（也就是前端传递过来的admin对象中的数据） 一定要返回自增id
        shop.setAdmin(admin);
        shopMapper.save(shop);
        //修改employee里面的shop_id
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);


    }
}
